package com.ggbz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ggbz.common.utils.JwtUtil;
import com.ggbz.common.utils.Result;
import com.ggbz.entity.Users;
import com.ggbz.entity.dto.LoginRequest;
import com.ggbz.entity.dto.UserUpdateRequest;
import com.ggbz.entity.vo.LoginResponse;
import com.ggbz.entity.vo.UserVO;
import com.ggbz.service.UsersService;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表，存储所有用户的基本信息 前端控制器
 * </p>
 *
 * @author ggbz
 * @since 2025-03-08
 */
@RestController
@RequestMapping("/users")
@ApiOperation("用户控制类")
public class UsersController {
    @Autowired
    UsersService usersService;


    // 用户注销（通过Token获取用户ID）
    @PostMapping("/logout")
    public Result logout(@RequestHeader("Authorization") String token) {

        try {
            // 从Token解析用户ID
            String userId = (String) JwtUtil.getTokenBody(token).get("userId");
            // 执行删除操作
            usersService.deleteUser(Long.valueOf(userId));

            return Result.ok().message("用户注销成功");
        } catch (RuntimeException e) {
            // 根据异常类型返回不同错误码
            String msg = e.getMessage();
            if (msg.contains("无效的Token格式") || msg.contains("Token解析失败")) {
                return Result.error().code(401).message(msg);
            } else if (msg.contains("用户不存在")) {
                return Result.error().code(404).message(msg);
            }
            return Result.error().code(500).message("系统错误");
        }
    }

    @GetMapping()
    public Result getUserById(@RequestHeader("Authorization") String token) {
        try {
            String userId = (String) JwtUtil.getTokenBody(token).get("userId");
            Users user = usersService.getUserById(Long.valueOf(userId));
            return Result.ok()
                    .message("查询成功")
                    .data("user", user); // 使用data方法添加用户数据
        } catch (RuntimeException e) {
            return Result.error()
                    .code(HttpStatus.SC_NOT_FOUND) // 使用Apache的404状态码
                    .message(e.getMessage());
        }
    }

    @PutMapping()
    public Result updateUser(
            @RequestHeader("Authorization") String token,
            @RequestBody UserUpdateRequest request) {
        try {
            String userId = (String) JwtUtil.getTokenBody(token).get("userId");
            usersService.updateUser(Long.valueOf(userId), request);
            return Result.ok()
                    .code(200)
                    .message("用户信息更新成功");
        } catch (RuntimeException e) {
            return Result.error()
                    .code(404)
                    .message(e.getMessage());
        }
    }

    // 用户登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request) {
        try {
            LoginResponse response = usersService.login(request);
            return Result.ok()
                    .data("LoginResponse",response); // 直接传入对象会存入data字段
        } catch (RuntimeException e) {
            return Result.error()
                    .code(401)
                    .message(e.getMessage());
        }
    }



}

